/**
 * Copyright (C) 2012 The SkyTvOS Project
 *
 * Version     Date           Author
 * ─────────────────────────────────────
 *           2015-2-14         yellowlgx
 *
 */

package com.skyworth.ui.menu;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.text.TextUtils.TruncateAt;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnFocusChangeListener;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationSet;
import android.view.animation.ScaleAnimation;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.skyworth.commen.ui.R;
import com.skyworth.theme.SkyThemeEngine;
import com.skyworth.util.MyFocusFrame;
import com.skyworth.util.SkyScreenParams;

/**
 * @ClassName SkyMenuItem
 * @Description TODO (write something)
 * @author yellowlgx
 * @date 2015-2-14
 * @version TODO (write something)
 */
public class SkyMenuItem extends FrameLayout implements OnFocusChangeListener
{
    private Context mContext;
    private LinearLayout contentLayout;

    private FrameLayout iconBglayout; // icon背景圆框，当有焦点的时候需要影藏，无焦点的时候显示
    private ImageView itemFocusView;// icon焦点view，当前为焦点，需要显示
    private ImageView itemUnfocusView;// icon非焦点view，当前不是焦点，或者负焦点时显示
    private int focusIconID = -1, unFocusIconID = -1, iconBgID = -1;

    private TextView itemTextView; // 显示内容
    private int textSize = 0;

    private SkyMenuData currentData;

    private boolean isSelect = false;

    private MyFocusFrame menuFocusView;

    public SkyMenuItem(Context context)
    {
        super(context);
        mContext = context;
        initView();
        this.setFocusable(true);
        this.setOnFocusChangeListener(this);
    }

    // 初始化控件
    private void initView()
    {
        contentLayout = new LinearLayout(mContext);
        // contentLayout.setPadding(SkyScreenParams.getInstence(mContext).getResolutionValue(10), 0,
        // SkyScreenParams.getInstence(mContext).getResolutionValue(45), 0);
        contentLayout.setPadding(SkyScreenParams.getInstence(mContext).getResolutionValue(10), 0, 0,
                0);
        contentLayout.setOrientation(LinearLayout.HORIZONTAL);

        FrameLayout iconLayout = new FrameLayout(mContext);
        LinearLayout.LayoutParams iconParams = new LinearLayout.LayoutParams(
                LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        iconParams.gravity = Gravity.CENTER_VERTICAL;
        contentLayout.addView(iconLayout, iconParams);

        iconBglayout = new FrameLayout(mContext);
        iconLayout.addView(iconBglayout, new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT));
        iconBglayout.setVisibility(View.GONE);

        itemFocusView = new ImageView(mContext);
        iconLayout.addView(itemFocusView,
                new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

        itemUnfocusView = new ImageView(mContext);
        iconLayout.addView(itemUnfocusView,
                new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

        itemTextView = new TextView(mContext);
        itemTextView.setSingleLine(true);
        itemTextView.getPaint().setAntiAlias(true);
        itemTextView.setEllipsize(TruncateAt.END);
        itemTextView.setMarqueeRepeatLimit(-1);
        // itemTextView.setPadding(SkyScreenParams.getInstence(mContext).getResolutionValue(25), 0,
        // 0,
        // 0);
        itemTextView.setGravity(Gravity.CENTER_VERTICAL);
        LinearLayout.LayoutParams title_p = new LinearLayout.LayoutParams(
                SkyScreenParams.getInstence(mContext).getResolutionValue(200),
                LayoutParams.MATCH_PARENT);
        title_p.leftMargin = SkyScreenParams.getInstence(mContext).getResolutionValue(25);
        contentLayout.addView(itemTextView, title_p);
        this.addView(contentLayout,
                new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
    }

    public void setFocusView(MyFocusFrame menuFocusView)
    {
        this.menuFocusView = menuFocusView;
    }

    /**
     * 设置文字内容属性
     * 
     * @param size
     *            大小
     * @param color
     *            颜色
     * @date 2015-2-14
     */
    public void setTextAttribute(int size, int color)
    {
        if (itemTextView != null)
        {
            textSize = size;
            itemTextView.setTextSize(size);
            itemTextView.setTextColor(getResources().getColor(color));
        }
    }

    public void setSelectState(boolean select)
    {
        isSelect = select;
    }

    public void refreshItemValue(SkyMenuData data)
    {
        if (data != null)
        {
            currentData = data;
            if (data.getItemTitle() != null)
                itemTextView.setText(data.getItemTitle());
//                itemTextView.setText(data.getItemTitle() + "　");

            if (data.getItemFocusIcon() != -1)
                focusIconID = data.getItemFocusIcon();
            if (data.getItemUnFocusIcon() != -1)
                unFocusIconID = data.getItemUnFocusIcon();
            if (data.getItemIconBg() != -1)
                iconBgID = data.getItemIconBg();

            this.post(new Runnable()
            {
                @Override
                public void run()
                {
                    int x = SkyScreenParams.getInstence(mContext).getResolutionValue(77)
                            - SkyScreenParams.getInstence(mContext).getResolutionValue(47);
                    int y = SkyScreenParams.getInstence(mContext).getResolutionValue(516);
                    int width = SkyMenuItem.this.getWidth()
                            + SkyScreenParams.getInstence(mContext).getResolutionValue(47) * 2;
                    int height = SkyScreenParams.getInstence(mContext).getResolutionValue(171);

                    // menuFocusView.changeFocusPos(SkyMenuItem.this);
                }
            });
        }
    }

    public void updataItemValue(SkyMenuData data)
    {
        if (data != null)
        {
            currentData = data;
            if (data.getItemTitle() != null)
                itemTextView.setText(data.getItemTitle());
//                itemTextView.setText(data.getItemTitle() + "　");
            itemTextView.setTextColor(getResources().getColor(R.color.c_3));

            iconBgID = data.getItemIconBg();
            focusIconID = data.getItemFocusIcon();
            unFocusIconID = data.getItemUnFocusIcon();

            iconBglayout.setBackground(SkyThemeEngine.getInstance().getDrawable(iconBgID, false));
//            itemUnfocusView.setBackgroundResource(unFocusIconID);
            itemUnfocusView.setBackground(SkyThemeEngine.getInstance().getDrawable(unFocusIconID, true));
            itemFocusView.setBackgroundResource(focusIconID);

            if (!isSelect)
            {
                iconBglayout.setVisibility(View.VISIBLE);
                itemUnfocusView.setVisibility(View.VISIBLE);
                itemFocusView.setVisibility(View.GONE);
            } else
            {
                iconBglayout.setVisibility(View.VISIBLE);
                itemUnfocusView.setVisibility(View.VISIBLE);
                itemFocusView.setVisibility(View.GONE);
            }
            if (itemUnfocusView.getBackground() != null)
            {
                Drawable image;
                image = itemUnfocusView.getBackground();
                image.mutate().setAlpha(200);
                itemUnfocusView.setBackgroundDrawable(image);
            }
            if (iconBglayout.getBackground() != null)
            {
                Drawable image;
                image = iconBglayout.getBackground();
                image.mutate().setAlpha(200);
                iconBglayout.setBackgroundDrawable(image);
            }
        }
    }

    public SkyMenuData getItemData()
    {
        return currentData;
    }

    public void setItemStateForSecond(boolean show)
    {
        if (!isSelect)
            if (show)
            {
                AlphaAnimation alphaAnim = new AlphaAnimation(0.6f, 0.3f);
                alphaAnim.setDuration(200);
                alphaAnim.setFillAfter(true);
                contentLayout.startAnimation(alphaAnim);
            } else
            {
                AlphaAnimation alphaAnim = new AlphaAnimation(0.3f, 1.0f);
                alphaAnim.setDuration(200);
                alphaAnim.setFillAfter(true);
                contentLayout.startAnimation(alphaAnim);
            }
    }

    public void resetvIconBgState()
    {
        if (iconBglayout != null)
        {
            iconBglayout.clearAnimation();
            // iconBglayout.setVisibility(View.GONE);
            iconBglayout.animate().alpha(0.0f).scaleX(0.0f).scaleY(0.0f).setDuration(100 * 3 / 2);
            Log.v("lgx", "iconBglayout.getVisibility == " + iconBglayout.getVisibility());
        }
    }

    public void focus()
    {
        // AnimationSet animSet = new AnimationSet(false);
        // ScaleAnimation scaleAnim = new ScaleAnimation(1.0f, 0.1f, 1.0f, 0.1f,
        // Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        // scaleAnim.setDuration(100);
        // animSet.addAnimation(scaleAnim);

        AlphaAnimation alphaAnim = new AlphaAnimation(1.0f, 0.1f);
        alphaAnim.setDuration(100 * 3 / 2);
        // animSet.addAnimation(alphaAnim);
        // iconBglayout.startAnimation(alphaAnim);

        iconBglayout.clearAnimation();
        iconBglayout.animate().alpha(0.0f).scaleX(0.0f).scaleY(0.0f).setDuration(100 * 3 / 2);
        // iconBglayout.setVisibility(View.GONE);
        alphaAnim.setAnimationListener(new AnimationListener()
        {
            @Override
            public void onAnimationStart(Animation animation)
            {
            }

            @Override
            public void onAnimationRepeat(Animation animation)
            {
            }

            @Override
            public void onAnimationEnd(Animation animation)
            {
                iconBglayout.setVisibility(View.GONE);
                iconBglayout.clearAnimation();
            }
        });

        itemUnfocusView.setVisibility(View.GONE);
        itemFocusView.setVisibility(View.VISIBLE);
        itemTextView.setTextColor(getResources().getColor(R.color.a_0));
        if (itemFocusView.getBackground() != null)
        {
            Drawable image;
            image = itemFocusView.getBackground();
            image.mutate().setAlpha(255);
            itemFocusView.setBackgroundDrawable(image);
        }
    }

    public void unfocus()
    {
        if (isSelect)
        {
            AnimationSet animSet = new AnimationSet(false);
            ScaleAnimation scaleAnim = new ScaleAnimation(0.1f, 1.0f, 0.1f, 1.0f,
                    Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
            scaleAnim.setDuration(100 * 3 / 2);
            animSet.addAnimation(scaleAnim);

            AlphaAnimation alphaAnim = new AlphaAnimation(0.1f, 1.0f);
            alphaAnim.setDuration(100 * 3 / 2);
            animSet.addAnimation(alphaAnim);

            iconBglayout.animate().alpha(1.0f).scaleX(1.0f).scaleY(1.0f).setDuration(100 * 3 / 2);

            // iconBglayout.startAnimation(animSet);
            animSet.setAnimationListener(new AnimationListener()
            {
                @Override
                public void onAnimationStart(Animation animation)
                {
                }

                @Override
                public void onAnimationRepeat(Animation animation)
                {
                }

                @Override
                public void onAnimationEnd(Animation animation)
                {
                    iconBglayout.setVisibility(View.VISIBLE);
                    iconBglayout.clearAnimation();
                }
            });
            itemUnfocusView.setVisibility(View.VISIBLE);
            itemFocusView.setVisibility(View.GONE);
            itemTextView.setTextColor(getResources().getColor(R.color.c_3));
            if (itemUnfocusView.getBackground() != null)
            {
                Drawable image;
                image = itemUnfocusView.getBackground();
                image.mutate().setAlpha(255);
                itemUnfocusView.setBackgroundDrawable(image);
            }
            if (iconBglayout.getBackground() != null)
            {
                Drawable image;
                image = iconBglayout.getBackground();
                image.mutate().setAlpha(200);
                iconBglayout.setBackgroundDrawable(image);
            }
        } else if (!isSelect)
        {
            AnimationSet animSet = new AnimationSet(false);
            ScaleAnimation scaleAnim = new ScaleAnimation(0.1f, 1.0f, 0.1f, 1.0f,
                    Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
            scaleAnim.setDuration(100 * 3 / 2);
            animSet.addAnimation(scaleAnim);

            AlphaAnimation alphaAnim = new AlphaAnimation(0.1f, 1.0f);
            alphaAnim.setDuration(100 * 3 / 2);
            animSet.addAnimation(alphaAnim);

            // iconBglayout.startAnimation(animSet);
            iconBglayout.animate().alpha(1.0f).scaleX(1.0f).scaleY(1.0f).setDuration(100 * 3 / 2);
            animSet.setAnimationListener(new AnimationListener()
            {
                @Override
                public void onAnimationStart(Animation animation)
                {
                }

                @Override
                public void onAnimationRepeat(Animation animation)
                {
                }

                @Override
                public void onAnimationEnd(Animation animation)
                {
                    iconBglayout.setVisibility(View.VISIBLE);
                    iconBglayout.clearAnimation();
                }
            });
            itemUnfocusView.setVisibility(View.VISIBLE);
            itemFocusView.setVisibility(View.GONE);
            itemTextView.setTextColor(getResources().getColor(R.color.c_3));
            if (itemUnfocusView.getBackground() != null)
            {
                Drawable image;
                image = itemUnfocusView.getBackground();
                image.mutate().setAlpha(200);
                itemUnfocusView.setBackgroundDrawable(image);
            }
            if (iconBglayout.getBackground() != null)
            {
                Drawable image;
                image = iconBglayout.getBackground();
                image.mutate().setAlpha(200);
                iconBglayout.setBackgroundDrawable(image);
            }
        }
    }

    public void refreshOnThemeChanged()
    {
        if (this.hasFocus())
        {
            itemTextView.setTextColor(getResources().getColor(R.color.a_0));
        } else
        {
            itemTextView.setTextColor(getResources().getColor(R.color.c_3));
            iconBglayout.setBackground(SkyThemeEngine.getInstance().getDrawable(iconBgID, false));
            itemUnfocusView.setBackground(SkyThemeEngine.getInstance().getDrawable(unFocusIconID, true));
        }
    }

    @Override
    public void onFocusChange(View v, boolean hasFocus)
    {
        if (hasFocus)
        {
            Log.v("xw", " focus  size ==  " + textSize);
            contentLayout.clearAnimation();
            focus();
            itemTextView.setEllipsize(TruncateAt.MARQUEE);
            itemTextView.setSelected(true);
            int x = -SkyScreenParams.getInstence(mContext).getResolutionValue(36);
            int y = SkyScreenParams.getInstence(mContext).getResolutionValue(496);
            // int width = SkyMenuItem.this.getWidth()
            // + SkyScreenParams.getInstence(mContext).getResolutionValue(83) * 2;
            // int height = SkyScreenParams.getInstence(mContext).getResolutionValue(266);
            int width = SkyScreenParams.getInstence(mContext).getResolutionValue(358)
                    + SkyScreenParams.getInstence(mContext).getResolutionValue(83) * 2;
            int height = SkyScreenParams.getInstence(mContext).getResolutionValue(100)
                    + SkyScreenParams.getInstence(mContext).getResolutionValue(83) * 2;

            // menuFocusView.changeFocusPos(x, y, width, height);
        } else
        {
            Log.v("xw", " unfocus  size ==  " + textSize);
            Log.v("xw", " itemTextView  " + itemTextView.getText().toString());
            itemTextView.setSelected(false);
            itemTextView.setEllipsize(TruncateAt.END);
            unfocus();
        }
    }
}
